package Cowquisition;

import java.util.ArrayList;

/**
 * WorldMap class
 *
 * Because there is already a Map class...
 * This stores the map of the world (it's a small world).
 *
 * @author Ryan Novak
 */
public class WorldMap
{
  /** The height of the map */
  private int mapHeight;
  /** The width of the map */
  private int mapWidth;
  /** A 2d list of the Tiles on the map */
  private ArrayList<ArrayList<Tile>> map;
  /** A 2d array of obstructions on the map */
  private Tile.objectType[][] obstructed;
  /** A 2d array of cows on the map */
  private int[][] cow;


  /**
   * Constructor for WorldMap
   *
   * @param height The height of the map
   * @param width The width of the map
   */
  public WorldMap(int height, int width)
  {
    mapHeight = height;
    mapWidth = width;

    obstructed = new Tile.objectType[mapHeight][mapWidth];
    cow = new int[mapHeight][mapWidth];  
    map = new ArrayList<ArrayList<Tile>>();

    for(int i = 0; i < mapWidth; i++)
      map.add(new ArrayList<Tile>());
    for(int i = 0; i < mapWidth; i++)
      for(int j = 0; j < mapHeight; j++)
        obstructed[i][j] = Tile.objectType.AIR;

    for(int i = 0; i < mapWidth; i++)
      for(int j = 0; j < mapHeight; j++)
        cow[i][j] = 0;
  }

  /**
   * setTile method
   *
   * Sets a Tile.<br>For simplicity, only the row can be specified. All Tiles should be added in order from left to right.
   *
   * @param row The row the Tile should be added to.
   * @param tileIn the Tile object to be put in that location.
   */
  public void setTile(int row, Tile tileIn)
  {
    map.get(row).add(tileIn);
  }

  /**
   * setObject method
   *
   * Puts an obstruction on the map.
   *
   * @param x The x coordinate of the obstruction.
   * @param y The y coordinate of the obstruction.
   * @param obstructionType The type of the obstruction.
   */
  public void setObject(int x, int y, Tile.objectType obstructionType)
  {
    obstructed[x][y] = obstructionType;
  }

  /**
   * getTile method
   *
   * Returns a Tile object in a given location on the map.
   *
   * @param x The x coordinate of the Tile to retrieve.
   * @param y The y coordinate of the Tile to retrieve.
   * @return Returns a Tile object in the specified location.
   */
  public Tile getTile(int x, int y)
  {
    if(cow[x][y] == 1)
      return Cowquisition.cow1;
    if(cow[x][y] == 2)
      return Cowquisition.cow2;
    if(cow[x][y] == 3)
      return Cowquisition.cow3;
    return map.get(x).get(y);
  }
  
  /**
   * getObject method
   *
   * Returns a value of the objectType enum for the given location on the map.
   *
   * @param x
   * @param y
   * @return Returns an obstruction type in the specified location.
   */
  public Tile.objectType getObject(int x, int y)
  {
    return obstructed[x][y];
  }
  
  /**
   * canWalk method
   *
   * Tells you if you can move onto the spacified location on the map.
   *
   * @param x The x coordinate to test.
   * @param y The y coordinate to test.
   * @return Returns true if an entity can inhabit the specified location.
   */
  public boolean canWalk(int x, int y)
  {
    return (obstructed[x][y] == Tile.objectType.AIR) && (cow[x][y] == 0) && (map.get(x).get(y).canWalk());
  }
  
  /**
   * setCow method
   *
   * Sets a cow.
   *
   * @param x The x location of the cow.
   * @param y The y location of the cow.
   * @param type The type (color) of the cow.
   */
  public void setCow(int x, int y, int type)
  {
    cow[x][y] = type;
  }
  
  /**
   * desetCow method
   *
   * Removes a cow.
   *
   * @param x The x location of the cow.
   * @param y The y location of the cow.
   */
  public void desetCow(int x, int y)
  {
    cow[x][y] = 0;
  }
}
